@extends('admin.layout.layout', ['row' => @$page])
@php
    $deleteRight = checkMethodsAuthority('roll_delete');

@endphp

@section('contain')



    <div class="portlet">
        <div class="portlet-title">
            <div class="caption">
                <x-portlet-breadcrumb :page="$page"></x-portlet-breadcrumb>
            </div>
            <div class="actions">
                <!--<div class="item">
                    <a href="/admin/download?a=rolls_distribution&b=template" class="btn btn-primary">{{ ts('DOWNLOADDISTTEMPLATE') }}</a>
                </div>
                <div class="item">
                    <a href="/admin/rolls/importDistribution" class="btn btn-primary">{{ ts('BULKDISTIMPORT') }}</a>
                </div>-->
                    @if (!empty($menus) && in_array('download_template', $menus) && 0)
                    <div class="item">
                        <a href="/admin/download?a=rolls&b=template" class="btn btn-primary">{{ ts('DOWNLOADTEMPLATE') }}</a>
                    </div>
                    @endif
                    @if (!empty($menus) && in_array('batch_export', $menus) && 0)
                    <div class="item">
                        <a href="/admin/rolls/import" class="btn btn-primary">{{ ts('BULKIMPORT') }}</a>
                    </div>
                    @endif
                    @if (!empty($menus) && in_array('add', $menus))
                    <div class="item">
                        <a href="/{{ request()->path() }}/create" class="btn btn-primary">{{ ts('addnew') }}</a>
                    </div>
                    @endif
                    @if (!empty($menus) && in_array('export', $menus))
                        <div class="item">
                            <button class="btn btn-primary" onclick="exportRolls()">{{ ts('EXPORT') }}</button>
                        </div>
                    @endif
            </div>
        </div>

        <div class="filter-wrapper">
            <form action="/{{ request()->path() }}/" class="form-inline filter-form">
                <div class=" input-group mb-3 custom-control-inline">
                    <input type="text" class="form-control s-input" name="value" value="{{ request()->input('value') }}" placeholder="{{ ts('ROLLSSEARCHTEXT') }}">
                </div>

                <x-p-f-select></x-p-f-select>

                <div class="input-group mb-3 custom-control-inline">
                    <select name="status" class="form-control">
                        <option value="">{{ ts('select').' '.ts('status') }}</option>
                        @foreach(range(0,2) as $v)
                            <option value="{{ $v }}" {{ (request()->input('status') !== null && request()->input('status') == $v) ? 'selected' : '' }}>{{ ts('RMST'.$v) }}</option>
                        @endforeach

                    </select>
                </div>

                <x-d-d-chain-select></x-d-d-chain-select>
                {{--<x-distributor-select></x-distributor-select>--}}
                <x-flatpickr-date-range></x-flatpickr-date-range>
                <div class="mb-3">
                    <button class="btn btn-primary">{{ ts('SEARCH') }}</button>
                    <a href="/{{ request()->path() }}/" class="btn btn-warning">{{ ts('RESET') }}</a>
                </div>
            </form>
        </div>
        <div>
            <div>
                <table class="table text-nowrap bt-table d-none text-center">
                    <thead>
                    <tr class="order_column text-center">
                        <th class="text-center">{{ ts('SELECT') }}</th>
                        <th data-column="id" style="width: 80px">{{ ts('SEQUENCE') }}</th>
                        <th data-column="roll_number" style="width: 150px">{{ ts('rollnumber') }}</th>
                        <th data-column="film_type_id" style="width: 150px">{{ ts('FILMTYPE') }}</th>
                        <th data-column="length">{{ ts('ROLLLENGTH') }}</th>
                        <th>{{ ts('剩余可分配长度') }}</th>
                        <th style="width: 400px">{{ ts('status') }}</th>
                        <th data-column="date_entered" style="width: 400px">{{ ts('DATESOLD') }}</th>
                        <th class="fixed-td" style="width:270px">
                            {{ ts('OPERATIONS') }}
                        </th>
                    </tr>
                    </thead>
                    <tbody>
                    @foreach($data as $k => $v)
                        @php
                            // $vRestlen = getRlenV401($v->roll_number) ?: 0;
                            $vRestlen = @$v->restlen ?: 0;
                            $Restlen =  getRestlenV301($v->roll_number) ?: 0;
                        @endphp
                        @if(session('hightlight') && session('hightlight.type')=='rolls' && session('hightlight.id')==$v->id)
                            <tr class="item-list table-warning">
                        @else
                            <tr class="item-list">
                                @endif

                                <td class="text-center">
                                    <input type="checkbox" class="roll_icheck icheck_input" name="ids[]" value="{{ $v->id }}">
                                </td>
                                <td>
                                    {{ @$data->firstItem()+$k }}
                                </td>
                                <td>
                                    {{ @$v->roll_number }}
                                </td>

                                <td>
                                    {{ ts('film_type_master.'.$v->film_type_id) }}
                                </td>

                                <td>
                                    {{ @$v->length }} cm
                                </td>
                                <td>
                                    {{ $vRestlen }}  cm
                                </td>
                                <td>
                                    {{ ts('RMST'.@$v->status) }}
                                    {{--@if($v->restlen == $v->length)
                                        {{ ts('unallocated') }}
                                    @elseif( $Restlen==$v->length )
                                        {{ ts('FP3') }}
                                    @else
                                        {{ ts('allocated') }}
                                    @endif--}}
                                </td>
                                <td>
                                    {{ !empty($v->rs_create_at) ? fdate2($v->rs_create_at) : '-' }}
                                </td>
                                <td  class="fixed-td text-center" style="width:270px">
                                    @if (!empty($menus) && in_array('look', $menus))
                                    <a class="btn btn-success btn-sm" href='/{{ request()->path() }}/{{ $v->id }}'>{{ ts('VIEW') }}</a>
                                    @endif
                                    @if (!empty($menus) && in_array('assign', $menus))
                                        @if( @$vRestlen > 0 )
                                            <button type="button" class="btn btn-primary btn-sm" onclick="openRollsAssignByRollNumber('{{ $v->roll_number }}')">{{ ts('ASSIGN') }}</button>
                                        @else
                                            <button type="button" disabled class="btn btn-secondary btn-sm" onclick="openRollsAssignByRollNumber('{{ $v->roll_number }}')">{{ ts('ASSIGN') }}</button>
                                        @endif
                                    @endif
                                    @if (!empty($menus) && in_array('edit', $menus))
                                        @empty($v->status)
                                            <a class="btn btn-warning btn-sm" href='/{{ request()->path() }}/{{ $v->id }}/edit'>{{ ts('edit') }}</a>
                                        @else
                                            <button class="btn btn-secondary btn-sm" disabled>{{ ts('edit') }}</button>
                                        @endif
                                    @endif
                                    @if (!empty($menus) && in_array('delete', $menus))
                                        @empty($v->status)
                                            <span class="btn btn-danger btn-sm" onclick="handleDelete3('{{ $v->id }}', '膜卷 {{ @$v->roll_number }}')">{{ ts('delete') }}</span>
                                        @else
                                            <button type="button" class="btn btn-secondary btn-sm" disabled>{{ ts('delete') }}</button>
                                        @endif
                                    @endif

                                </td>
                            </tr>
                            @endforeach
                    </tbody>
                </table>
            </div>
            <div class="row my-3">
                @if(@$deleteRight)
                    <div class="col-auto">
                        <label style="cursor: pointer"><input type="checkbox" class="icheck_all" data-ele=".roll_icheck"><span class="ml-2">{{ ts('SELECTALL') }}</span></label>
                        <span class="btn btn-primary ml-3" onclick="deleteAlliCheck('.roll_icheck', '/admin/rolls/deletes')">{{ ts('delete') }}</span>
                    </div>
                @endif
                <div class="col">
                    {{ !empty($data) ? $data->appends(request()->input())->links() : '' }}
                </div>
            </div>
        </div>

        <form class="modal fade modal-s1" id="rollsAssign">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title font-green">{{ ts('ASSIGNROLL') }}</h5>
                        <button type="button" class="close" data-dismiss="modal">&times;</button>
                    </div>
                    <div class="modal-body">
                        <div class="form-item">
                            <div class="form-label" required="">{{ ts('rollnumber') }}</div>
                            <div class="form-input">
                                <input type="text" readonly name="roll_id" class="form-control">
                                <div class="text-danger residue-length mt-3">{{ ts('RESIDUELENGTH') }} <b></b> cm</div>
                            </div>
                        </div>
                        <div class="form-item">
                            <div class="form-label" required="">{{ ts('distributor') }}</div>
                            <div class="form-input">
                                <select class="form-control sdid" name="sdid" style="width: 100%;">
                                    @foreach(gDV1() as $v)
                                        <option value="{{ @$v->id }}">{{ @$v->abbr ?: @$v->company_name }}</option>
                                    @endforeach
                                </select>
                            </div>
                        </div>
                        <div class="form-item">
                            <div class="form-label" required="">{{ ts('assigntouser') }}</div>
                            <div class="form-input">
                                <select class="form-control user_id" is_show="1" act="admin"  name="user_id" style="width: 100%;"></select>
                            </div>
                        </div>
                        <div class="form-item">
                            <div class="form-label" required="">{{ ts('length') }}</div>
                            <div class="form-input">
                                <div class="input-group custom-control-inline">
                                    <div class="input-group-prepend">
                                        <label class="input-group-text">
                                            <input type="checkbox" class="icheck_input" name="all_length" value="1">
                                            <span class="ml-1">{{ ts('ALL') }}</span>
                                        </label>
                                    </div>
                                    <input type="number" class="form-control" name="length">
                                    <div class="input-group-append">
                                        <label class="input-group-text">
                                            <span>cm</span>
                                        </label>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="py-3 text-center">
                            <button type="button" class="btn btn-primary" onclick="handleRollsAssign()">{{ ts('assign') }}</button>
                        </div>
                    </div>
                </div>
            </div>
        </form>

    </div>


@endsection


@section('script')
    <script>
        $(function () {
           $(".sdid, .user_id").select2();
        });
        $('body').on('change', '[name="sdid"]', function () {
            var val=$(this).val();
            var $that = $(this);
                $('.portlet select[name="user_id"]').empty();
                request.get('/admin/ajax/getUsers', {
                    params: {
                        t: 'd',
                        user_id: val
                    }
                })
                    .then(function (d) {
                        var data = d.data
                        $('.portlet select[name="user_id"]').empty().append(makeSelectOptionsOnly(data))
                    })
                    .catch(function (d) {
                        toastr.error(d.msg)
                    })
        });

        function getDealers() {
            var val = $('.distributor_json[name="distributor_id"]').val();
            var dealer_id = $('.dealer_json[name="dealer_id"]').data('value');
            if (val) {
                request.get('/admin/ajax/getUsers', {
                    params: {
                        t: 'd',
                        user_id: val
                    }
                })
                    .then( function(d) {
                        var data = d.data
                        // makeSelectOptionsOnly
                        $('.dealer_json[name="dealer_id"]').empty().append(makeSelectOptions(data, dealer_id))
                    })
                    .catch(function (d) {
                        toastr.error(d.msg)
                    })
            }
        }

        function exportRolls() {
            // console.log('export');
            var a = Qs.parse(location.search.replace('?', ''));
            var form = Object.assign({}, a,  {
                m: 1
            });
            var url = '/admin/rolls/export?' + Qs.stringify(form)+location.hash;
            window.open(url)
        }

        $(document).ready(function () {
            $(".distributor_json").select2();
            $(".dealer_json").select2();
            $(".filmtype_json").select2();
            if ($('#rollsAssign [name="sdid"]').length >=1 ) {
                    request.get('/admin/ajax/getUsers', {
                        params: {
                            t: 'd',
                            user_id: $('[name="sdid"]').val()
                        }
                    })
                        .then(function (d) {
                            var data = d.data;
                            $('.portlet select[name="user_id"]').empty().append(makeSelectOptionsOnly(data))
                        })
                        .catch(function (d) {
                            toastr.error(d.msg)
                        })
            }
            $('.table.bt-table').bootstrapTable({
                fixedColumns: true,
                fixedNumber: 1,
                fixedRightNumber: 1,
            }).removeClass('d-none');

            $('#rollsAssign .icheck_input[name="all_length"]').on('ifChecked', function(event){
                var max = $('#rollsAssign').find('.residue-length b').text();
                $('#rollsAssign [name="length"]').val(max);
            });
            getDealers();
            $('.distributor_json[name="distributor_id"]').on('change', getDealers)
        })
    </script>
@endsection
